<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Articles extends Controller_Common {
	public $template='layout/index';

	public function action_index() {
		$per_page = $this->_config->get('items_per_page');
		$articles = Model::factory('Articles')->getSome($this->getType($this->request->param('type')), $this->getOffset($per_page));

		if (!empty($articles)) {
			$this->buildCarcas();
			$this->template->title = $this->sitetext['comapny_name']['_value'].' - '.$this->sitetext[$this->request->param('type')]['_value'];
			$this->template->content = View::factory('pages/articles', array(
				'title' => $this->sitetext[$this->request->param('type')]['_value'],
				'articles' => $articles,
				'items_count' => count($articles),
	            'pagination' => Pagination::factory(array(
	            	'view' => 'pagination/basic_ru',
	                'total_items' => Model::factory('articles')->countAll($this->getType($this->request->param('type'))),
	                'items_per_page' => $this->_config->get('items_per_page'),
	            ))
			));
		} else {
			throw new HTTP_Exception_404(':page does not exist', array(':page' => 'Gaia'));
		}
	}

	public function action_one() {
		$article = Model::factory('Articles')->getOne($this->request->param('id'));

		if (!empty($article)) {
			$this->buildCarcas();
			$this->template->title = $this->sitetext['comapny_name']['_value'].' - '.$this->sitetext[$this->request->param('type')]['_value'].' - '.$article->title;
			$this->template->description = $article->meta_description;
			$this->template->content = View::factory('pages/article', array(
				'article' => $article,
			));
		} else {
			throw new HTTP_Exception_404(':page does not exist', array(':page' => 'Gaia'));
		}
	}

	private function getType($str) {
		switch ($str) {
			case 'news':
				return 0;
				break;
			case 'services':
				return 1;
				break;
			case 'projects':
				return 2;
				break;
			case 'partners':
				return 3;
				break;
		}
	}
}